package test3;

import apple.laf.JRSUIUtils;

import javax.swing.tree.TreeNode;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;

public class Solution {
    public TreeNode mirrorTree(TreeNode root) {
        // 在此处写入代码
        if (root == null) {
            return null;
        }
        TreeNode left = root.left;
        root.left = root.right;
        root.right = left;
        mirrorTree(root.left);
        mirrorTree(root.right);
        return root;
    }

    public List<Integer> rightSideView(TreeNode root) {
        // 在此处写入代码
        List<Integer> result = new ArrayList<>();

        if (root == null) {
            return result;
        }
        LinkedList<TreeNode> list = new LinkedList<>();
        list.add(root);

        int nodeCount = 1;

        while (!list.isEmpty()) {
            result.add(list.getLast().val);
            while (nodeCount > 0) {
                TreeNode first = list.pollFirst();
                if (first.left != null) {
                    list.add(first.left);
                }
                if (first.right != null) {
                    list.add(first.right);
                }

                nodeCount --;
            }

            nodeCount = list.size();
        }
        return result;

    }

    public int maxPathSum(TreeNode root) {
        // 在此处写入代码
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            return root.val;
        }
        int left = maxPathSum(root.left);
        int leftValue = Math.max(left, 0);
        int right = maxPathSum(root.right);
        int rightValue = Math.max(right, 0);
        return Math.max(Math.max(leftValue, rightValue), root.val + left + right);

    }


    // Definition for a binary tree node.
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) { val = x; }
    }
}
